﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Users
{
    public class Mail_Template_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/Mail_Template_Edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        public IMail_templateService _mail_template { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_mail_template", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            this.action = Request.Query["action"];

            if (!string.IsNullOrEmpty(this.action) && this.action == CMSEnums.ActionEnum.Edit.ToString())
            {
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_mail_template.Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Action = this.action;
            ViewBag.Id = this.id.ToString();
        }
        //
        // GET: /admin/Mail_Template_Edit/

        public ActionResult Index()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString())
            {
                ShowInfo(this.id);
            }
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_mail_template", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                }
                else
                {
                    JscriptMsg("修改邮件模板成功！", "../mail_template_list/index");
                }
            }
            else //添加
            {
                ChkAdminLevel("user_mail_template", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                }
                else
                {
                    JscriptMsg("添加邮件模板成功！", "../mail_template_list/index");
                }
            }
            return View(EDIT_RESULT_VIEW);
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            var model = _mail_template.GetModel(_id);
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Mail_template model = new Mail_template();

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.call_index = Request.Form["txtCallIndex"].FirstOrDefault().Trim();
            model.maill_title = Request.Form["txtMailTitle"].FirstOrDefault().Trim();
            model.content = Request.Form["txtContent"];

            if (_mail_template.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加邮件模板:" + model.title); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            var model = _mail_template.GetModel(_id);

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.call_index = Request.Form["txtCallIndex"].FirstOrDefault().Trim();
            model.maill_title = Request.Form["txtMailTitle"].FirstOrDefault().Trim();
            model.content = Request.Form["txtContent"];

            if (_mail_template.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改邮件模板:" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion
    }
}
